package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.StudentInfoMapper;
import com.project.entity.*;
import com.project.entity.Class;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 15:04
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class StudentInfoServiceImpl extends BaseService implements StudentInfoService {
    @Autowired
    UniversityService universityService;
    @Autowired
    SchoolService schoolService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    MajorService majorService;
    @Autowired
    ClassService classService;
    @Autowired
    TeacherInfoService teacherInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private CrypDataHelper crypDataHelper;
    @Autowired
    private StudentInfoMapper studentInfoMapper;



    @Override
    public int modifyByUserIdSelective(StudentInfo studentInfo) {
        return studentInfoMapper.updateByUserIdSelective(studentInfo);
    }

    @Override
    public StudentInfo findOneByUserId(Integer userId) {
        return studentInfoMapper.selectByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 根据id修改学生信息表
     *
     * @param studentInfo
     * @return
     */
    @Override
    public int modifyOneById(StudentInfo studentInfo) {
        return studentInfoMapper.updateByPrimaryKeySelective(studentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @Override
    public StudentInfo findById(Integer id) {
        return studentInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/7/1
     * function: 根据userId查找学生详情信息
     *
     * @param userId
     * @return
     */
    @Override
    public StudentInfoDetail findDetailByUserId(Integer userId) throws Exception {
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        Class clazz = null;
        User user;
        studentInfoDetail.setUser(userService.findOneByUserID(userId));
        StudentInfo studentInfo = findOneByUserId(userId);
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo != null) {
            studentInfoDetail.setStudentInfo(studentInfo);
            if (studentInfo.getClassid() != null) {
                clazz = classService.findOne(studentInfo.getClassid());
                studentInfoDetail.setClazz(clazz);
            }
            //实习指导老师
            if (studentInfo.getInternadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findByUserIdUnDecry(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoInternName(crypDataHelper.decRealName(user.getRealname()));
                }
            }
            //新指导老师（更换中指导老师）
            //实习指导老师
            if (studentInfo.getNewadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getNewadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findByUserIdUnDecry(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoNewInternName(crypDataHelper.decRealName(user.getRealname()));
                }
            }

            //毕设指导老师
            if (studentInfo.getPaperadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getPaperadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findByUserIdUnDecry(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoPaperName(crypDataHelper.decRealName(user.getRealname()));
                }
            }
            if (clazz != null) {
                if (clazz.getMajorid() != null)
                    studentInfoDetail.setMajor(majorService.findOne(clazz.getMajorid()));
                if (clazz.getDepartmentid() != null)
                    studentInfoDetail.setDepartment(departmentService.findOne(clazz.getDepartmentid()));
                if (clazz.getSchoolid() != null)
                    studentInfoDetail.setSchool(schoolService.findOne(clazz.getSchoolid()));
                if (clazz.getUniversityid() != null)
                    studentInfoDetail.setUniversity(universityService.findOne(clazz.getUniversityid()));
            }
        }
        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据学生信息表id查找学生详情信息
     *
     * @param id 学生信息表id
     * @return
     */
    @Override
    public StudentInfoDetail findDetailById(Integer id) throws Exception {
        User user;
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        StudentInfo studentInfo = findById(id);
        studentInfoDetail.setStudentInfo(studentInfo);
        studentInfoDetail.setUser(userService.findOneByUserID(studentInfo.getUserid()));
        Class clazz = null;
        if (studentInfo != null) {
            studentInfoDetail.setStudentInfo(studentInfo);
            if (studentInfo.getClassid() != null) {
                clazz = classService.findOne(studentInfo.getClassid());
                studentInfoDetail.setClazz(clazz);
            }
            //实习指导老师
            if (studentInfo.getInternadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findByUserIdUnDecry(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoInternName(crypDataHelper.decRealName(user.getRealname()));
                }
            }
            //毕设指导老师
            if (studentInfo.getAdvisestate() != null) {
                //先找到教师信息
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getPaperadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findByUserIdUnDecry(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoPaperName(crypDataHelper.decRealName(user.getRealname()));
                }
            }
            if (clazz != null) {
                if (clazz.getMajorid() != null)
                    studentInfoDetail.setMajor(majorService.findOne(clazz.getMajorid()));
                if (clazz.getDepartmentid() != null)
                    studentInfoDetail.setDepartment(departmentService.findOne(clazz.getDepartmentid()));
                if (clazz.getSchoolid() != null)
                    studentInfoDetail.setSchool(schoolService.findOne(clazz.getSchoolid()));
                if (clazz.getUniversityid() != null)
                    studentInfoDetail.setUniversity(universityService.findOne(clazz.getUniversityid()));
            }
        }
        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/7/6
     * function: 模糊查找 + 分页
     *
     * @param studentInfo
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByCondition(StudentInfo studentInfo) {
        return studentInfoMapper.selectByCondition(studentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/7/26
     * function: 根据学校实习指导老师的教师信息表id计算被指导的实习学生人数
     *
     * @param internAdvisor 学校实习指导老师的教师信息表id
     * @return
     */
    @Override
    public int countByInternAdvisor(Integer internAdvisor) {
        return studentInfoMapper.countByInternAdvisor(internAdvisor);
    }

    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 新增学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail 学生详细信息
     * @return
     */
    @Override
    public void addDetailOne(StudentInfoDetail studentInfoDetail) throws Exception {
        //update用户表
        if (studentInfoDetail.getUser() != null)
            userService.updateUser(studentInfoDetail.getUser());
        //insert学生信息表
        if (studentInfoDetail.getStudentInfo() != null)
            add(studentInfoDetail.getStudentInfo());
    }

    /**
     * author: WheelChen
     * date: 2017/7/29
     * function:  修改学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail
     * @return
     */
    @Override
    public void modifyDetailOne(StudentInfoDetail studentInfoDetail) throws Exception {
        //update用户表
        if (studentInfoDetail.getUser() != null)
            userService.updateUser(studentInfoDetail.getUser());
        //update学生信息表
        if (studentInfoDetail.getStudentInfo() != null)
            modifyOneById(studentInfoDetail.getStudentInfo());
    }

    /**
     * author: WheelChen
     * date: 2017/7/1
     * function: 根据学生信息表记录查找学生详情信息 （）
     *
     * @param studentInfo
     * @return
     */
    @Override
    public StudentInfoDetail findDetailByStudentInfo(StudentInfo studentInfo) throws Exception {
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo.getUserid() != null)
            studentInfoDetail.setUser(userService.findOneByUserID(studentInfo.getUserid()));
        else
            throw new Exception("学生信息表缺少userId");
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo.getDepartmentid() != null)
            studentInfoDetail.setDepartment(departmentService.findOne(studentInfo.getDepartmentid()));
        if (studentInfo.getMajorid() != null)
            studentInfoDetail.setMajor(majorService.findOne(studentInfo.getMajorid()));
        if (studentInfo.getClassid() != null)
            studentInfoDetail.setClazz(classService.findOne(studentInfo.getClassid()));
//        //实习指导老师
//        if (studentInfo.getInternadvisor() != null) {
//            //先找到教师信息表
//            TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
//            //再找到用户表
//            if (teacherInfo != null) {
//                user = userService.findOneByUserID(teacherInfo.getUserid());
//                studentInfoDetail.setTeacherInfoInternName(user.getRealname());
//            }
//        }


//        if (studentInfo.getSchoolid() != null)
//            studentInfoDetail.setSchool(schoolService.findOne(studentInfo.getSchoolid()));

        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/10/30
     * function: 根据学院id查找学生信息
     *
     * @param schoolId 学院id
     * @return 学生信息表List
     */
    @Override
    public List<StudentInfo> findBatchBySchoolId(Integer schoolId) {
        return studentInfoMapper.selectBatchBySchoolId(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据系id批量查找学生信息
     *
     * @param departmentId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByDepartmentId(Integer departmentId) {
        return studentInfoMapper.selectBatchByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据专业id批量查找学生信息
     *
     * @param majorId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByMajorId(Integer majorId) {
        return studentInfoMapper.selectBatchByMajorId(majorId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/16
     * function: 根据班级id查找学生信息
     *
     * @param classId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByClassId(Integer classId) {
        return studentInfoMapper.selectBatchByClassId(classId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据系id批量删除学生表信息
     *
     * @param departmentId
     * @return
     */
    @Override
    public int removeBatchByDepartmentId(Integer departmentId) {
        return studentInfoMapper.deleteBatchByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据专业id批量删除学生表信息
     *
     * @param majorId
     * @return
     */
    @Override
    public int removeBatchByMajorId(Integer majorId) {
        return studentInfoMapper.deleteBatchByMajorId(majorId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/16
     * function: 根据班级id批量删除学生表信息
     *
     * @param classId
     * @return
     */
    @Override
    public int removeBatchByClassId(Integer classId) {
        return studentInfoMapper.deleteBatchByClassId(classId);
    }

    /**
     * author: WheelChen
     * date: 2017/10/22
     * function: 根据id批量删除学生表信息
     *
     * @param idList
     * @return
     */
    @Override
    public int removeBatchByIdList(List idList) {
        return studentInfoMapper.deleteBatchByIdList(idList);
    }

//    @Override
//    public List<StudentInfoDetail> findDetailByCondition(StudentInfoDetail studentInfoDetail) {
//
//        return studentInfoMapper.selectDetailByCondition(studentInfoDetail);
//    }


//    @Override
//    public int save(String jsonStr) {
//        StudentInfo studentInfo = null;
//        University university = null;
//        School school = null;
//        Department department = null;
//        Major major = null;
//        com.project.entity.Class aClass= null;
//        TeacherInof teacherInofIntern = null;
//        TeacherInof teacherInofPaper = null;
//
//        JSONObject jsonObject = JSON.parseObject(jsonStr);
//        if (jsonObject.getString("studentInfo") != null)
//            studentInfo = JSONObject.parseObject(jsonObject.getString("studentInfo"), StudentInfo.class);
//        if (jsonObject.getString("university") != null)
//            university = JSONObject.parseObject(jsonObject.getString("university"), University.class);
//        if (jsonObject.getString("school") != null)
//            school = JSONObject.parseObject(jsonObject.getString("school"), School.class);
//        if (jsonObject.getString("department") != null)
//            department = JSONObject.parseObject(jsonObject.getString("department"), Department.class);
//        if (jsonObject.getString("major") != null)
//            major = JSONObject.parseObject(jsonObject.getString("major"), Major.class);
//        if (jsonObject.getString("aClass") != null)
//            aClass = JSONObject.parseObject(jsonObject.getString("aClass"), com.project.entity.Class.class);
//        if (jsonObject.getString("teacherInofIntern") != null)
//            teacherInofIntern = JSONObject.parseObject(jsonObject.getString("teacherInofIntern"), TeacherInof.class);
//        if (jsonObject.getString("teacherInofPaper") != null)
//            teacherInofPaper = JSONObject.parseObject(jsonObject.getString("teacherInofPaper"), TeacherInof.class);
//
//
//        modifyByUserIdSelective(studentInfo);
//
//        return 0;
//    }

    @Override
    public int add(StudentInfo studentInfo) {
        return studentInfoMapper.insertSelective(studentInfo);
    }

    @Override
    public int removeOneByUserId(Integer userId) {
        return studentInfoMapper.deleteByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/11/2
     * function: 根据自我介绍内容筛选对应大学下的学生
     *
     * @param schoolList 学院列表
     * @param studentInfo 学生信息表
     * @return
     */
    @Override
    public List<JSONObject> findBatchByConditionWithSchoolList(List<School>  schoolList, StudentInfo studentInfo) throws Exception {
        studentInfo.setSchoolList(schoolList);
        List<StudentInfo> studentInfoList = studentInfoMapper.selectBatchByConditionWithSchoolList(studentInfo);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (StudentInfo studentInfo1 : studentInfoList) {
            JSONObject jsonObject = new JSONObject();
            //学生表信息
            jsonObject.put("studentInfo", studentInfo1);
            //用户表信息
            User user = userService.findOneByUserID(studentInfo1.getUserid());
            jsonObject.put("user", user);
            //学校
            if (user != null)
                if (user.getUniversityid() != null){
                    jsonObject.put("university", universityService.findOne(user.getUniversityid()));
                }
            //学院
            if (studentInfo1.getSchoolid() != null)
                jsonObject.put("school", schoolService.findOne(studentInfo1.getSchoolid()));
            //系
            if (studentInfo1.getDepartmentid() != null)
                jsonObject.put("department", departmentService.findOne(studentInfo1.getDepartmentid()));
            //专业
            if (studentInfo1.getMajorid() != null)
                jsonObject.put("major", majorService.findOne(studentInfo1.getMajorid()));
            //班级
            if (studentInfo1.getClassid() != null)
                jsonObject.put("class", classService.findOne(studentInfo1.getClassid()));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

}
